﻿using System;
using System.Web;
using System.Data;

namespace msBsc.WebUi
{
    public partial class wfConfirmShipping : msBsc.Cls.BPCustom
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindControls();
                if (!string.IsNullOrEmpty(this.GetFormParam("ID")))
                {
                    doedit(GetFormParam("ID"));
                }
            }

        }
        private void BindControls()
        {
            if (!IsPostBack)
            {
                using (Epm.AppLogic.Framework.CDbAccess oacc = new Epm.AppLogic.Framework.CDbAccess())
                {
                    DataSet ds1 = oacc.QueryBySQL("select EXPRESS_CMP_NO,EXPRESS_CMP_NME from BSE_EXPRESS_CMP order by EXPRESS_CMP_NME asc");
                    this.BoundComboBox("dplLOGISTICS_COMPANY_ID", ds1, "EXPRESS_CMP_NME", "EXPRESS_CMP_NO", "", "EXPRESS_CMP_NME asc", true);
                }

                BoundFromProvince();
            }
        }


        private void BoundFromProvince()
        {
            using (Epm.AppLogic.Framework.CDbAccess oCDbAccess = new Epm.AppLogic.Framework.CDbAccess())
            {
                string strSql = "SELECT * FROM BSE_PROVINCE ";

                DataSet ds = oCDbAccess.QueryBySQL(strSql);
                this.BoundComboBox("dplPROVINCE_ID", ds, "PROVINCE_NME", "PROVINCE_ID", "", "", true);
            }
        }

        private void BoundFromCity(string strFatherId)
        {
            using (Epm.AppLogic.Framework.CDbAccess oCDbAccess = new Epm.AppLogic.Framework.CDbAccess())
            {
                string strSql = "SELECT * FROM BSE_PROVINCE_CITY WHERE (FATHER_ID='" + strFatherId + "')";

                DataSet ds = oCDbAccess.QueryBySQL(strSql);
                this.BoundComboBox("dplCITY_ID", ds, "CITY_NME", "CITY_ID", "", "", true);
            }
        }

        private void BoundArea(string strFatherId)
        {
            using (Epm.AppLogic.Framework.CDbAccess oCDbAccess = new Epm.AppLogic.Framework.CDbAccess())
            {
                string strSql = "SELECT * FROM BSE_PROVINCE_CITY_AREA WHERE (FATHER_ID='" + strFatherId + "')";

                DataSet ds = oCDbAccess.QueryBySQL(strSql);
                this.BoundComboBox("dplAREA_ID", ds, "AREA_NME", "AREA_ID", "", "", true);
            }
        }


        private void doedit(string strid)
        {
            using (msBsc.AppLogic.DataAlc.CBizShpOrderForm odataobj = new AppLogic.DataAlc.CBizShpOrderForm())
            {
                DataSet ds = odataobj.DataObj.QueryBySQL(@"
    SELECT TOP 1
    A.TO_PHONE_NO
    ,A.TO_LANDLINE_NO
    ,A.TO_ZONE_DESC
    ,A.TO_STREET_ADDRESS
    ,A.TO_RECEIPT_NME
    ,A.TRADE_STATUS_TXT
    ,D.*
    FROM SHP_ORDER_FORM A
    LEFT OUTER JOIN SHP_ORDER_FORM_LIST B ON A.ORDER_FORM_ID=B.ORDER_FORM_ID
    LEFT OUTER JOIN SHP_GOODS_INFO C ON B.GOODS_ID=C.GOODS_ID
    LEFT OUTER JOIN SHP_COMMERCIAL_SHIPPING D ON C.SHIP_ADDRESS_ID=D.SHIP_ADDRESS_ID
    WHERE
    A.ORDER_FORM_ID='" + strid + @"'
    ORDER BY (CASE WHEN D.SHIP_ADDRESS_ID IS NOT NULL THEN 1 ELSE 0 END) DESC
");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    this.ExecJScript("$('#txtID').val('" + strid + "');");

                    string strTel = ds.Tables[0].Rows[0]["TO_PHONE_NO"].ToString();
                    if (ds.Tables[0].Rows[0]["TO_LANDLINE_NO"].ToString() != string.Empty)
                    {
                        strTel += " " + ds.Tables[0].Rows[0]["TO_LANDLINE_NO"].ToString();
                    }
                    string strAddr = ds.Tables[0].Rows[0]["TO_ZONE_DESC"].ToString() + ds.Tables[0].Rows[0]["TO_STREET_ADDRESS"].ToString()
                        + " " + ds.Tables[0].Rows[0]["TO_RECEIPT_NME"].ToString() + "(" + strTel + ")";

                    this.ExecJScript("$('#spnReceiptInfo').html('" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(strAddr) + "');");

                    this.ExecJScript("$('#spnOrderSid').html('" + ds.Tables[0].Rows[0]["TRADE_STATUS_TXT"].ToString() + "');");


                    BoundFromCity(ds.Tables[0].Rows[0]["PROVINCE_ID"].ToString());
                    BoundArea(ds.Tables[0].Rows[0]["CITY_ID"].ToString());


                    this.AddPageRetRow("J_FormBox", ds.Tables[0].Rows[0]);

                    this.SetContrlValue("dplPROVINCE_ID", ds.Tables[0].Rows[0]["PROVINCE_ID"].ToString());
                    this.SetContrlValue("dplCITY_ID", ds.Tables[0].Rows[0]["CITY_ID"].ToString());
                    this.SetContrlValue("dplAREA_ID", ds.Tables[0].Rows[0]["AREA_ID"].ToString());
                }
                else
                {
                    ToErrPage("错误的引用，未选择订单！"); 
                }
            }
        }

        private void doSave()
        {
            if (this.GetPostHtValue("PROVINCE_ID").ToString() == string.Empty || this.GetPostHtValue("CITY_ID").ToString() == string.Empty || this.GetPostHtValue("AREA_ID").ToString() == string.Empty)
            {
                Alert("请选择所在地区！");
                return;
            }
            if (this.GetPostHtValue("STREET_ADDRESS").ToString() == string.Empty || this.GetPostHtValue("STREET_ADDRESS").ToString().Length < 5 || this.GetPostHtValue("STREET_ADDRESS").ToString().Length > 120)
            {
                Alert("请填写详细地址，不需要重复填写省市区，必须大于5个字符，小于120个字符！");
                return;
            }
            if (this.GetPostHtValue("ZIP_CODE").ToString() == string.Empty || this.GetPostHtValue("ZIP_CODE").ToString().Length != 6)
            {
                Alert("请填写邮政编码， 必须6个字符！");
                return;
            }
            if (this.GetPostHtValue("SHIPPER_NME").ToString() == string.Empty || this.GetPostHtValue("SHIPPER_NME").ToString().Length < 2 || this.GetPostHtValue("SHIPPER_NME").ToString().Length > 20)
            {
                Alert("请填写发货人，发货人姓名姓名应为2-20个字符，一个汉字为两个字符！");
                return;
            }
            if (this.GetPostHtValue("PHONE_NO").ToString() == string.Empty && this.GetPostHtValue("LANDLINE_NO").ToString() == string.Empty)
            {
                Alert("电话号码、手机号选填一项！");
                return;
            }
            if (this.GetPostHtValue("LANDLINE_NO").ToString() != string.Empty && this.GetPostHtValue("AREA_CODE").ToString() == string.Empty)
            {
                Alert("请填写电话号码区号！");
                return;
            }



            using (msBsc.AppLogic.DataAlc.CBizShpOrderForm odataobj = new AppLogic.DataAlc.CBizShpOrderForm())
            {
                System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
                transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
                transactionOption.Timeout = new TimeSpan(0, 10, 0);
                using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
                {
                    DataSet ds = odataobj.SelectByIds(this.GetParamHtValue("ID"));
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            DataRow drnew = ds.Tables[0].Rows[i];
                            if (drnew["TRADE_STATUS_TXT"].ToString() != "买家已付款")
                            {
                                continue;
                            }

                            drnew["FROM_PROVINCE_ID"] = this.GetPostHtValue("PROVINCE_ID");
                            drnew["FROM_CITY_ID"] = this.GetPostHtValue("CITY_ID");
                            drnew["FROM_AREA_ID"] = this.GetPostHtValue("AREA_ID");
                            drnew["FROM_ZONE_DESC"] = this.GetPostHtValue("ZONE_DESC");
                            drnew["FROM_STREET_ADDRESS"] = this.GetPostHtValue("STREET_ADDRESS");
                            drnew["FROM_ZIP_CODE"] = this.GetPostHtValue("ZIP_CODE");
                            drnew["FROM_SENDER_NME"] = this.GetPostHtValue("SHIPPER_NME");
                            drnew["FROM_PHONE_NO"] = this.GetPostHtValue("PHONE_NO");
                            drnew["FROM_LANDLINE_NO"] = "";
                            if (this.GetPostHtValue("LANDLINE_NO").ToString() != string.Empty)
                            {
                                drnew["FROM_LANDLINE_NO"] = this.GetPostHtValue("AREA_CODE").ToString() + "-" + this.GetPostHtValue("LANDLINE_NO").ToString();
                                if (this.GetPostHtValue("EXTENSION_NO").ToString() != string.Empty)
                                {
                                    drnew["FROM_LANDLINE_NO"] = drnew["FROM_LANDLINE_NO"].ToString() + "-" + this.GetPostHtValue("EXTENSION_NO").ToString();
                                }
                            }
                            drnew["LOGISTICS_COMPANY_ID"] = this.GetPostHtValue("LOGISTICS_COMPANY_ID");
                            drnew["EXPRESS_CMP_NME"] = this.GetPostHtValue("EXPRESS_CMP_NME");
                            drnew["DELIVER_TYPE_CID"] = "快递";// this.GetPostHtValue("DELIVER_TYPE_CID");
                            drnew["WAYBILL_NUMBER"] = this.GetPostHtValue("WAYBILL_NUMBER");
                            drnew["TRADE_STATUS_TXT"] = "卖家已发货";
                            drnew["DELIVER_DT"] = DateTime.Now;
                            drnew["AUTO_AFFIRM_DT"] = DateTime.Now.AddDays(9);
                        }
                        odataobj.Update(ds);
                    }
                    ts.Complete();
                }
            }
            this.ExecJScript("if(parent &&parent.doToPageNm){parent.doToPageNm();} if(parent &&parent.layer){var index = parent.layer.getFrameIndex(window.name);setTimeout(function(){parent.layer.close(index);},200);}");


        }

        public override void DoAction()
        {
            if (this.strAction == "dplPROVINCE_ID_change")
            {
                BoundFromCity(this.GetParamHtValue("PROVINCE_ID"));
            }
            else if (this.strAction == "dplCITY_ID_change")
            {
                BoundArea(this.GetParamHtValue("CITY_ID"));
            }
            else if (this.strAction == "doSave")
            {
                doSave();
            }
            //else if (this.strAction == "doedit")
            //{
            //    doedit(GetParamHtValue("ID"));
            //}
        }
    }
}